//
//  HNForgetPwdController.m
//  baidaifu
//
//  Created by 朱传正 on 2017/1/3.
//  Copyright © 2017年 haoniu. All rights reserved.
//

#import "HNForgetPwdController.h"

@interface HNForgetPwdController ()

@end

@implementation HNForgetPwdController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (IBAction)backAction:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)getCodeAction:(UIButton *)sender {
    
    
}



- (IBAction)findPwdAcrtion:(UIButton *)sender {
    if (![_tel.text isMobileNumber]) {
        [self showHint:@"手机号码格式不正确"];
        return;
    }
    if (!_code.text) {
        [self showHint:@"请先获取邀请码"];
        return;
    }
    
    if (![_code.text isVerifyNum:6]) {
        [self showHint:@"验证码错误"];
        return;
    }
    if (![_pwd.text isPassWord]) {
        [self showHint:@"密码长度在6~16位"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:_tel.text,@"username",_pwd.text,@"password",_code.text,@"captcha",nil];
    [param setObject:@"1" forKey:@"isDoctor"];
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_changePwd_code parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            //                    NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"obj"]];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            //                    NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            //                    [self showHint:info];
            [self showHint:@"密码修改成功"];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"密码修改失败"];
    }];

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){226.0/255.0,226.0/255.0,226.0/255.0,1});
    
    _codeView.layer.masksToBounds = YES;
    _codeView.layer.cornerRadius = 1;
    _codeView.layer.borderColor = color;
    _codeView.layer.borderWidth = 1;
    
    _telView.layer.masksToBounds = YES;
    _telView.layer.cornerRadius = 1;
    _telView.layer.borderColor = color;
    _telView.layer.borderWidth = 1;
    
    _pwdView.layer.masksToBounds = YES;
    _pwdView.layer.cornerRadius = 1;
    _pwdView.layer.borderColor = color;
    _pwdView.layer.borderWidth = 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)getVerifyCode{
    if (![_tel.text isMobileNumber]) {
        [self showHint:@"手机号码格式不正确"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:_tel.text,@"phone" ,nil];
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_code parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            
            //            [self showHint:@"获取验证码成功"];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"注册失败"];
    }];
    
}



@end
